package com.example.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Count;
import com.example.mapper.CountMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Author: luog
 * @Date: 2024/12/2
 */
@Service
public class CountServiceImpl extends ServiceImpl<CountMapper, Count> implements CountService {
    @Resource
    CountMapper countMapper;

    @Override
    public Integer getCount(HttpServletRequest hsr) {
        DateTime nowTime = DateUtil.date();
        Integer number = 1;
        String remoteAddr = getClientIp(hsr);
        LambdaQueryWrapper<Count> QueryWrapper = new LambdaQueryWrapper();
        QueryWrapper.eq(Count::getIp, remoteAddr);
        Count count = super.getOne(QueryWrapper);
        //存在就累加
        if (count != null) {
            countMapper.increaceCount(count.getId());
        } else {
            //不存在就创建
            Count saveCount = new Count();
            saveCount.setCount(number);
            saveCount.setIp(remoteAddr);
            saveCount.setLastLoginTime(nowTime);
            saveCount.setCreateTime(nowTime);
            countMapper.insert(saveCount);
        }
        return countMapper.sumCount();
    }

    /**
     * @Description: 获取ip
     * @Param: [request]
     * @return: java.lang.String
     * @Author: luog
     * @Date: 2024/12/2
     */
    public String getClientIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("X-Real-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("X-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("X-Cluster-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("CF-Connecting-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }

        // 如果是多级代理，那么取第一个非unknown的IP为客户端真实IP
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }

        return ipAddress;
    }

}
